package com.shiwaixiangcun.monitor.controller;

import com.shiwaixiangcun.core.plugin.dto.MonkeyDto;
import com.shiwaixiangcun.core.plugin.web.validator.ConstraintType;
import com.shiwaixiangcun.core.plugin.web.validator.ParameterConstraint;
import com.shiwaixiangcun.core.plugin.web.validator.ParameterConstraints;
import com.shiwaixiangcun.core.web.response.ResponseEntity;
import com.shiwaixiangcun.monitor.dto.WorkOrderQualityAllotRuleDto;
import com.shiwaixiangcun.monitor.dto.WorkOrderQualityRecordDto;
import com.shiwaixiangcun.monitor.enums.QualityStatus;
import com.shiwaixiangcun.monitor.service.WorkOrderQualityAllotRuleService;
import com.shiwaixiangcun.monitor.service.WorkOrderQualityRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * Created by Administrator on 2017/11/23.
 */
@Controller
@RequestMapping("/work/order/quality/")
public class WorkOrderQualityController {

    @Autowired
    private WorkOrderQualityAllotRuleService workOrderQualityAllotRuleService;
    @Autowired
    private WorkOrderQualityRecordService workOrderQualityRecordService;


    // --- --- ---  质检规则  --- --- ---
    @RequestMapping(value = "/rule/listdata", method = RequestMethod.GET)
    public List<WorkOrderQualityAllotRuleDto> ruleListData() {
        return workOrderQualityAllotRuleService.ruleList();
    }


    @ParameterConstraints({
            @ParameterConstraint(name = "categoryId", constraintType = {ConstraintType.NotNull}, notNull = @NotNull(message = "工单分类不能为空")),
            @ParameterConstraint(name = "userId", constraintType = {ConstraintType.NotNull}, notNull = @NotNull(message = "质检人不能为空"))
    })
    @RequestMapping(value = "/rule/add", method = RequestMethod.POST)
    public String ruleAdd(MonkeyDto monkeyDto) {
        workOrderQualityAllotRuleService.ruleAdd(monkeyDto);
        return "SUCCESS:添加成功";
    }

    @RequestMapping(value = "/rule/remove", method = RequestMethod.DELETE)
    public String ruleRemove(@RequestParam Long id) {
        workOrderQualityAllotRuleService.ruleRemove(id);
        return "SUCCESS:已成功移除";
    }


    // --- --- ---  质检记录  --- --- ---
    @RequestMapping(value = "/record/listpage/wait", method = RequestMethod.GET)
    public Page<WorkOrderQualityRecordDto> waitRecordListPage(Pageable pageable, MonkeyDto monkeyDto) {
        monkeyDto.put("status", QualityStatus.Wait.name());
        return workOrderQualityRecordService.recordList(pageable, monkeyDto);
    }

    @RequestMapping(value = "/record/listpage/judged", method = RequestMethod.GET)
    public Page<WorkOrderQualityRecordDto> judgedRecordListPage(Pageable pageable, MonkeyDto monkeyDto) {
        return workOrderQualityRecordService.recordList(pageable, monkeyDto);
    }


    @RequestMapping(value = "/record/judge", method = RequestMethod.PUT)
    public String recordJudge(@RequestParam Long id, @RequestParam QualityStatus status, String reason) {
        workOrderQualityRecordService.judge(id, status, reason);
        return "SUCCESS:已处理";
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public ResponseEntity detail(@RequestParam Long id) {
        return new ResponseEntity().success(workOrderQualityRecordService.getDetail(id));
    }
}
